Imports System.Data.SqlClient

Public Class Fia : Implements IBusquedaFiltrado
    Private idEstado As Integer
    Private numfia As Long
    Private nrodoc As String
    Private idtipodoc As Integer
    Private fecGen As Date
    Private fecEntrega As Date
    Private monto As Long
    Private idMuestra As Long
    Private matri As Integer
    Public Property matricula() As Integer
        Get
            Return matri
        End Get
        Set(ByVal value As Integer)
            matri = value
        End Set
    End Property
    Public Property Muestra() As Long
        Get
            Return idMuestra
        End Get
        Set(ByVal value As Long)
            idMuestra = value
        End Set
    End Property
    Public Property idEstF() As Integer
        Get
            Return idEstado
        End Get
        Set(ByVal value As Integer)
            idEstado = value
        End Set
    End Property
    Public Property MonEst() As Long
        Get
            Return monto
        End Get
        Set(ByVal value As Long)
            monto = value
        End Set
    End Property
    Public Property nrofia() As Long
        Get
            Return numfia
        End Get
        Set(ByVal value As Long)
            numfia = value
        End Set
    End Property
    Public Property nroDocFia() As String
        Get
            Return nrodoc
        End Get
        Set(ByVal value As String)
            nrodoc = value
        End Set
    End Property
    Public Property tipodocFia() As Integer
        Get
            Return idtipodoc
        End Get
        Set(ByVal value As Integer)
            idtipodoc = value
        End Set
    End Property
    Public Property FecGenFia() As Date
        Get
            Return fecGen
        End Get
        Set(ByVal value As Date)
            fecGen = value
        End Set
    End Property
    Public Property fecEntregaFia() As Date
        Get
            Return fecEntrega
        End Get
        Set(ByVal value As Date)
            fecEntrega = value
        End Set
    End Property
    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO FIA (numfia,nrodoc,idtipodoc,idMuestra,fechageneracion,fechaentrega,matricula,monto,IdEstado)" + _
              "  VALUES({0},'{1}',{2},{3},'{4}','{5}',{6},{7},{8})"
        sql = String.Format(sql, Me.nrofia, Me.nroDocFia, Me.tipodocFia, Me.Muestra, Format(Me.FecGenFia, "dd/MM/yyyy"), Format(Me.fecEntregaFia, "dd/MM/yyyy"), Me.matricula, Me.MonEst, Me.idEstF)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Buscar
        Dim comando As SqlDataAdapter
        Dim rs As DataTableReader
        Dim dato As DataTable
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM FIA WHERE numfia ={0} ", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("fia")
        comando.Fill(dato)
        rs = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()

        If rs.Read Then
            nrofia = System.Convert.ToInt16(rs("numfia"))
            idtipodoc = rs("idtipodoc")
            nrodoc = rs("nrodoc")
            fecGen = rs("fechageneracion")
            fecEntrega = rs("fechaentrega")
            matricula = rs("matricula")
            Muestra = rs("idMuestra")
            monto = rs("monto")
            idEstado = rs("idestado")
            lResp = True
        Else
            lResp = False
            MsgBox("El nro de FIA no existe")
        End If
        rs.Close()
        Return lResp
    End Function
    
    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Eliminar
        Return True
    End Function
    Public Function ModiMonto(ByVal fia As Integer, ByRef con As ConexionBD) As Boolean
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE FIA " + _
              "  SET  NUMFIA= {0}," + _
              "      NRODOC ='{1}'," + _
              "      IDTIPODOC ={2}," + _
              "      IDMUESTRA ={3}," + _
              "      FECHAGENERACION ='{4}'," + _
              "      FECHAENTREGA='{5}'," + _
              "      MATRICULA ={6}," + _
              "      MONTO={7}," + _
              "      IDESTADO={8}" + _
              "  WHERE NUMFIA={0}"
        sql = String.Format(sql, Me.nrofia, Me.nroDocFia, Me.tipodocFia, Me.Muestra, Format(Me.FecGenFia, "dd/MM/yyyy"), Format(Me.fecEntregaFia, "dd/MM/yyyy"), Me.matricula, Me.MonEst, Me.idEstF)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Return True
    End Function

    Public Sub New()
        idEstado = 0
        nrofia = 0
        nrodoc = ""
        idtipodoc = 0
        fecGen = Nothing
        fecEntrega = Nothing
        monto = 0
        matri = 0
        idMuestra = 0
    End Sub
    Public Function BuscarDatos(ByVal id As Integer, ByVal numdoc As String, ByRef con As ConexionBD) As DataSet
        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = String.Format("SELECT * FROM FIA WHERE idTipoDoc={0} and nroDoc='{1}'", id, numdoc)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "fia")

        Return datos
    End Function
    Public Function BuscarFia(ByVal xFia As Integer, ByRef con As ConexionBD) As DataSet
        Dim datos As DataSet = New DataSet()
        Dim adapter As SqlDataAdapter
        Dim sql As String = ""
        sql = String.Format("SELECT distinct D.idestudio,E.nombre FROM DETALLEFIA D,ESTUDIO E WHERE D.idestudio=E.idestudio and D.numFia ={0}", xFia)
        sql = String.Format(sql, xFia)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "detallefia")
        Return datos
    End Function
    Public Function BuscarModi(ByVal xfia As Integer, ByVal ida As Integer, ByVal ide As Integer, ByRef con As ConexionBD) As Boolean

        'Dim comando As SqlCommand
        'Dim rs As SqlDataReader
        'Dim sql As String
        'Dim lResp As Boolean = False

        'sql = String.Format("SELECT * FROM BARRIO WHERE Descrip='{0}'", DescBar)
        'comando = New SqlCommand(sql, con.ConexionBase)
        'rs = comando.ExecuteReader()
        'comando.Dispose()


        'If rs.Read Then
        '    idBarrio = System.Convert.ToInt16(rs("idBarrio"))
        '    descri = rs("Descrip").ToString
        '    lResp = True
        'Else
        '    lResp = False
        'End If
        'rs.Close()
        'Return lResp

    End Function
End Class
